<?php  if(!defined('BASEPATH')) exit('No direct script access allowed');

    Class User_model extends CI_Model{
        
        public function __construct(){
            parent::__construct();
            $this->user = 'users';
            $this->key = 'keys';
        }
        
        public function addKey($data){
            $this->db->insert($this->key,$data);
            $affect = $this->db->affected_rows();
            if($affect){
                return true;
            }else{
                return false;
            }   
        }

        public function updateKey($data,$condition){
            $this->db->where($condition);
            $this->db->update($this->key,$data);
            $affect = $this->db->affected_rows();
            if($affect){
                return true;
            }else{
                return false;
            }  
        }

        function auth_token($key){
            $this->db->select();
            $this->db->from($this->key);
            $this->db->where('key', $key);
            $this->db->limit(1);
            
            $query = $this->db->get();
            
            if($query->num_rows() == 1){
                return $query->row_array();
            }else{
                return false;
            }
        } 

        public function getListUser($limit=null, $offset=null){
            $this->db->select();
            $session_data = $this->session->userdata('logged_in');
            $id = $session_data['id']; 
            $this->db->where('iduser !=', $id);
            $data = $this->db->get($this->user);
            $data = $data->result_array();
            return $data;
        }
        
        public function addUser($data){
            $this->db->insert($this->user,$data);
            $affect = $this->db->affected_rows();
            if($affect){
                return true;
            }else{
                return false;
            }   
        }
        
        public function getSingleUser($condition){
            $data = $this->db->select();
            $this->db->where($condition);
            $data = $data->get($this->user);
            $data = $data->row_array(); 
            return $data;
        }
        
        public function changePass($data,$condition){
            $this->db->where($condition);
            $this->db->update($this->user,$data);
            $affect = $this->db->affected_rows();
            if($affect){
                return true;
            }else{
                return false;
            }  
        }
        
        public function deleteUser($condition){
            $this->db->where($condition);
            $this->db->delete($this->user);
            $affect = $this->db->affected_rows();
            if($affect){
                return true;
            }else{
                return false;
            }   
        }
        
        function login($username, $password){
            $this->db->select('iduser, username, password');
            $this->db->from('users');
            $this->db->where('username', $username);
            $this->db->where('password', MD5($password));
            $this->db->where('active', 1);
            $this->db->limit(1);
            
            $query = $this->db->get();
            
            if($query->num_rows() == 1){
                return $query->result();
            }else{
                return false;
            }
        }  
    }
?>

